package com.zhuoyue.common.api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuoyue.biz.core.api.dao.SysThirdDataMapper;
import com.zhuoyue.biz.core.api.domain.third.JpushDO;
import com.zhuoyue.biz.core.api.domain.third.SmsDO;
import com.zhuoyue.common.xml.XmlParseUtil;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 短信http接口的java代码调用
 *
 * @author wjming
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class JavaSmsApi {
    /**
     * 智能匹配模版发送接口的http地址
     */
    private final String URI_SEND_SMS = "http://yunpian.com/v1/sms/send.json";

    /**
     * 模板发送接口的http地址
     */
    private final String URI_TPL_SEND_SMS = "http://yunpian.com/v1/sms/tpl_send.json";

    /**
     * 编码格式。发送编码格式统一用UTF-8
     */
    private final String ENCODING = "UTF-8";


    private final SysThirdDataMapper thirdDataMapper;

    /**
     * 通过模板发送短信
     *
     * @param tplValue 模板变量值
     * @param mobile   接受的手机号
     * @return json格式字符串
     * @throws IOException
     */

    public String tplSendSms(String tplValue, String mobile, String organizationCode) throws IOException {
        Map<String, String> params = new HashMap<>(5);
        try {
            SmsDO smsDo = thirdDataMapper.selectSmsMessage(organizationCode);
            params.put("apikey", smsDo.getDataKey());
            params.put("tpl_id", String.valueOf(smsDo.getTemplateId()));
            params.put("tpl_value", tplValue);
            params.put("mobile", mobile);

        } catch (Exception e) {
            System.out.println("JavaSmsApi.tplSendSms:" + e);
        }
        return post(URI_TPL_SEND_SMS, params);
    }

    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url       提交的URL
     * @param paramsMap 提交<参数，值>Map
     * @return 提交响应
     */
    public String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return responseText;
    }
}
